<template>
    <div class="login">
        <div class="login_form">
            <h3>登录</h3>
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0" class="login-form">
                <el-form-item prop="userName">
                    <el-input type="username" v-model="ruleForm.userName" prefix-icon="iconfont el-icon-user"></el-input>
                </el-form-item>
                <el-form-item prop="userWord">
                    <el-input type="password" v-model="ruleForm.passWord" prefix-icon="iconfont el-icon-lock"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm">登录</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      ruleForm: {
        userName: '',
        passWord: ''
      },
      rules: {
        user: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在3到8位之间', trigger: 'blur' }
        ],
        pass: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 12, message: '长度在6到12位之间', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm () {
      if (this.ruleForm.userName.length && this.ruleForm.passWord.length) {
        this.$api.login(this.ruleForm).then(res => {
          console.log(res)
          if (res.data.code === 1) {
            window.sessionStorage.setItem('token', res.data.data.token)
            window.sessionStorage.setItem('auth', res.data.data.data.auth)
            this.$router.push('/home')
            this.$message.success(res.data.message)
          }
        })
      } else {
        this.$message.error('账号密码不能为空')
      }
    },

    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped>
.login{
    width: 100%;
    height: 100%;
    position: relative;
    background: darkgray;
}
.login_form{
    width: 500px;
    height: 300px;
    padding: 0 20px;
    position: absolute;
    top: 30%;
    left: 50%;
    background: #fff;
    transform: translate(-50%,-30%);
}
.login_form>h3{
    width: 100%;
    height: 50px;
    text-align: center;
    line-height: 50px;
}
</style>
